package cn.ee.dfs.lotto.controller;

import cn.ee.dfs.common.annotation.Log;
import cn.ee.dfs.common.controller.BaseController;
import cn.ee.dfs.common.entity.DfsConstant;
import cn.ee.dfs.common.entity.DfsResponse;
import cn.ee.dfs.common.entity.QueryRequest;
import cn.ee.dfs.common.exception.DfsException;
import cn.ee.dfs.common.utils.DfsUtil;
import cn.ee.dfs.lotto.lottery.SuperLottery;
import cn.ee.dfs.lotto.service.ISuperLotteryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 大乐透 Controller
 *
 * @author TWJ
 * @date 2024-02-04 16:18:29
 */
@Slf4j
@Validated
@Controller
public class SuperLotteryController extends BaseController {

    @Autowired
    private ISuperLotteryService superLotteryService;

    @GetMapping(DfsConstant.VIEW_PREFIX + "superLottery")
    private String superLotteryIndex() {
        return DfsUtil.view("superLottery/superLottery");
    }

    @GetMapping("dlt")
    @ResponseBody
    @RequiresPermissions("superLottery:list")
    public DfsResponse getAllSuperLotterys(SuperLottery superLottery) {
        return new DfsResponse().success().data(superLotteryService.findSuperLotterys(superLottery));
    }

    @GetMapping("dlt/list")
    @ResponseBody
    @RequiresPermissions("superLottery:list")
    public DfsResponse superLotteryList(QueryRequest request, SuperLottery superLottery) {
        Map<String, Object> dataTable = getDataTable(this.superLotteryService.findSuperLotterys(request, superLottery));
        return new DfsResponse().success().data(dataTable);
    }

    @Log("新增SuperLottery")
    @PostMapping("dlt")
    @ResponseBody
    @RequiresPermissions("superLottery:add")
    public DfsResponse addSuperLottery(@Valid SuperLottery superLottery) throws DfsException {
        try {
            this.superLotteryService.createSuperLottery(superLottery);
            return new DfsResponse().success();
        } catch (Exception e) {
            String message = "新增SuperLottery失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }

    @Log("删除SuperLottery")
    @GetMapping("dlt/delete")
    @ResponseBody
    @RequiresPermissions("superLottery:delete")
    public DfsResponse deleteSuperLottery(SuperLottery superLottery) throws DfsException {
        try {
            this.superLotteryService.deleteSuperLottery(superLottery);
            return new DfsResponse().success();
        } catch (Exception e) {
            String message = "删除SuperLottery失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }

    @Log("拉取数据")
    @PostMapping("dlt/pull")
//    @RequiresPermissions("superLottery:pull")
    @ResponseBody
    public DfsResponse pullSuperLotteryData(@RequestBody Map<String, Object> queryParam) throws DfsException {
        try {
            this.superLotteryService.pullSuperLotteryData(queryParam);
            return new DfsResponse().success();
        } catch (Exception e) {
            String message = "删除SuperLottery失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }

    @Log("分割数据")
    @PostMapping("dlt/split")
//    @RequiresPermissions("superLottery:pull")
    @ResponseBody
    public DfsResponse splitLotteryData(@RequestBody Map<String, Object> queryParam) throws DfsException {
        try {
            this.superLotteryService.splitLotteryData(queryParam);
            return new DfsResponse().success();
        } catch (Exception e) {
            String message = "分割SuperLottery失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }

    @Log("更新最新数据")
    @GetMapping("dlt/update/{count}")
//    @RequiresPermissions("superLottery:pull")
    @ResponseBody
    public DfsResponse updateCurrentLotteryData(@PathVariable int count) throws DfsException {
        try {
            this.superLotteryService.updateCurrentLotteryData(count);
            return new DfsResponse().success();
        } catch (Exception e) {
            String message = "更新最新数据失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }


    @Log("修改SuperLottery")
    @PostMapping("dlt/update")
    @ResponseBody
    @RequiresPermissions("superLottery:update")
    public DfsResponse updateSuperLottery(SuperLottery superLottery) throws DfsException {
        try {
            this.superLotteryService.updateSuperLottery(superLottery);
            return new DfsResponse().success();
        } catch (Exception e) {
            String message = "修改SuperLottery失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }

    @RequestMapping("dlt/excel")
    @ResponseBody
//    @RequiresPermissions("superLottery:export")
    public void export(QueryRequest queryRequest, SuperLottery superLottery, HttpServletResponse response) throws DfsException {
        try {
//            List<SuperLottery> superLotterys = this.superLotteryService.findSuperLotterys(queryRequest, superLottery).getRecords();
            QueryWrapper<SuperLottery> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("lotteryDrawNum");

            ExcelKit.$Export(SuperLottery.class, response).downXlsx(this.superLotteryService.list(queryWrapper), false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }
}
